use std::sync::Arc;
use dashmap::DashMap;
use crate::{CmdStrategy, Redis, RespFrame, Session, SimpleString};

pub struct SelectCmd;


impl CmdStrategy for SelectCmd {
    fn execute(&self, fragments: Vec<&str>, db: Redis, sesseion_id: String, sessions: Arc<DashMap<String, Session>>) -> Result<RespFrame, String> {
        let index = fragments[4].parse::<usize>().unwrap();

        if index < 0 || index > 15 {
            return Err("index out of range".to_string());
        }

        sessions.entry(sesseion_id).and_modify(|session| {
            session.db_index = index;
        });

        let str = SimpleString::new("Ok");

        let resp = RespFrame::SimpleString(str);
        Ok(resp)
    }
}